package com.mes.loco.aps.server.controller.sch;

import com.iplant.aps.controller.BaseController;
import com.iplant.aps.service.SCHService;
import com.iplant.aps.service.po.ServiceResult;
import com.iplant.aps.service.po.bms.BMSDepartment;
import com.iplant.aps.service.po.bms.BMSEmployee;
import com.iplant.aps.service.po.sch.SCHSecondmentBPM;
import com.iplant.aps.service.utils.StringUtils;
import com.iplant.aps.utils.RetCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 借调(流程引擎版)
 *
 * @author PengYouWang
 * 2020-7-24 15:24:10
 */
@RestController
@RequestMapping("/api/SCHSecondmentBPM")
public class SCHSecondmentBPMController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(SCHSecondmentBPMController.class);

    final
    SCHService wSCHService;

    public SCHSecondmentBPMController(SCHService wSCHService) {
        this.wSCHService = wSCHService;
    }

    /**
     * 查历史记录
     */
    @GetMapping("/History")
    public Object History(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));
            String wCode = StringUtils.parseString(request.getParameter("Code"));
            int wUpFlowID = StringUtils.parseInt(request.getParameter("UpFlowID"));
            int wFollowerID = StringUtils.parseInt(request.getParameter("FollowerID"));
            int wType = StringUtils.parseInt(request.getParameter("Type"));
            int wPersonID = StringUtils.parseInt(request.getParameter("PersonID"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));

            ServiceResult<List<SCHSecondmentBPM>> wServiceResult = wSCHService.SCH_QuerySecondmentBPMHistory(wLoginUser,
                    wID, wCode, wUpFlowID, wFollowerID, wPersonID, wType, wStartTime, wEndTime);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 人员获取任务
     */
    @GetMapping("/EmployeeAll")
    public Object EmployeeAll(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wTagTypes = StringUtils.parseInt(request.getParameter("TagTypes"));
            Calendar wStartTime = StringUtils.parseCalendar(request.getParameter("StartTime"));
            Calendar wEndTime = StringUtils.parseCalendar(request.getParameter("EndTime"));
            int wType = StringUtils.parseInt(request.getParameter("Type"));

            ServiceResult<List<SCHSecondmentBPM>> wServiceResult = wSCHService
                    .SFC_QuerySecondementBPMEmployeeAll(wLoginUser, wTagTypes, wStartTime, wEndTime, wType);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 查单条
     */
    @GetMapping("/Info")
    public Object Info(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            // 获取参数
            int wID = StringUtils.parseInt(request.getParameter("ID"));

            ServiceResult<SCHSecondmentBPM> wServiceResult = wSCHService.SCH_GetSecondementBPM(wLoginUser, wID);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 获取人员班组、工位、岗位列表
     */
    @GetMapping("/PersonInfo")
    public Object PersonInfo(HttpServletRequest request) {
        Map<String, Object> wResult = new HashMap<>();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<Integer> wServiceResult = wSCHService.SCH_QueryPersonInfo(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", null, wServiceResult.Result);

                if (wServiceResult.CustomResult.containsKey("OldClass")) {
                    this.SetResult(wResult, "OldClass", wServiceResult.CustomResult.get("OldClass"));
                }

                if (wServiceResult.CustomResult.containsKey("OldPartList")) {
                    this.SetResult(wResult, "OldPartList", wServiceResult.CustomResult.get("OldPartList"));
                }

                if (wServiceResult.CustomResult.containsKey("OldPositionList")) {
                    this.SetResult(wResult, "OldPositionList", wServiceResult.CustomResult.get("OldPositionList"));
                }

                if (wServiceResult.CustomResult.containsKey("EmployeeList")) {
                    this.SetResult(wResult, "EmployeeList", wServiceResult.CustomResult.get("EmployeeList"));
                }
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }

    /**
     * 根据登录人信息，获取所在工区的部门列表（排除登录人所在部门）
     */
    @GetMapping("/QueryDepartmentListByLogin")
    public Object QueryDepartmentListByLogin(HttpServletRequest request) {
        Object wResult = new Object();
        try {
            if (CheckCookieEmpty(request)) {
                wResult = GetResult(RetCode.SERVER_CODE_UNLOGIN, "");
                return wResult;
            }

            BMSEmployee wLoginUser = GetSession(request);

            ServiceResult<List<BMSDepartment>> wServiceResult = wSCHService.SCH_QueryDepartmentListByLogin(wLoginUser);

            if (StringUtils.isEmpty(wServiceResult.FaultCode)) {
                wResult = GetResult(RetCode.SERVER_CODE_SUC, "", wServiceResult.Result, null);
            } else {
                wResult = GetResult(RetCode.SERVER_CODE_ERR, wServiceResult.FaultCode);
            }
        } catch (Exception ex) {
            logger.error(ex.toString());
            wResult = GetResult(RetCode.SERVER_CODE_ERR, ex.toString(), null, null);
        }
        return wResult;
    }
}
